<?php 
ob_start();
session_start(); // on relaye la session 
if(isset($_SESSION["is_registered"]) && $_SESSION["is_registered"] == "registered"){
		$enregistrer = $_SESSION["is_registered"];
}else{
	header("location:../../../menu.php"); // redirection en cas d'echec
}
require_once('../../../Connections/microassur.php');
require_once('../../../utils/fonctions.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
 
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")){ 
$datedebut = datevalider($_POST['Date_debut_indemnisation']);
$datevalide1 = datevalide($_POST['Date_debut_indemnisation']);
$lesdates = verifDate($_POST['Date_debut_indemnisation'],$_POST['Date_fin_indemnisation']);
$bool = verifLibelle1('Code_indemnisation') && verifLibelle('Type_indemnisation') &&  verifLibelle('Identifiant_formule') && verifLibelle('Identifiant_garantie') && verifNumeric($_POST['Montant_indemnisation']) && $datedebut &&	$datevalide1;
// $existe1 = existeChamp($database_microassur, $microassur,'indemnisation','Code_indemnisation',@$_POST['Code_indemnisation']);
$toCriterias['Code_indemnisation'] = $_POST['Code_indemnisation'] ;
$toCriterias['Type_indemnisation'] = $_POST['Type_indemnisation'] ;
$existe1 = existeEnregistrement($database_microassur, $microassur, "indemnisation", $toCriterias) ;

	if(isset($_POST['Date_fin_indemnisation']) && $_POST['Date_fin_indemnisation']!=NULL){
	$datefin = datevalider($_POST['Date_fin_indemnisation']);
	$datevalide2 = datevalide($_POST['Date_fin_indemnisation']);
	$bool=$bool && $datefin && $datevalide2;
	}
//	var_dump($bool);
	if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")  && $bool && !$existe1 && verifDate($_POST['Date_debut_indemnisation'],$_POST['Date_fin_indemnisation'])) 
	{ 
		  $insertSQL = sprintf("INSERT INTO indemnisation (Identifiant_indemnisation, Code_indemnisation, Type_indemnisation, Montant_indemnisation, Date_debut_indemnisation, Date_fin_indemnisation, Identifiant_garantie, Identifiant_formule) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
							   GetSQLValueString($_POST['Identifiant_indemnisation'], "int"),
							   GetSQLValueString($_POST['Code_indemnisation'], "text"),
							   GetSQLValueString($_POST['Type_indemnisation'], "text"),
							   GetSQLValueString($_POST['Montant_indemnisation'], "int"),
							   GetSQLValueString(dateEN2FR($_POST['Date_debut_indemnisation']), "date"),
							   GetSQLValueString(dateEN2FR($_POST['Date_fin_indemnisation']), "date"),
							   GetSQLValueString($_POST['Identifiant_garantie'], "int"),
							   GetSQLValueString($_POST['Identifiant_formule'], "int"));

			mysql_select_db($database_microassur, $microassur);
			mysql_query("START TRANSACTION");
			$Result1 = mysql_query($insertSQL, $microassur) or die(mysql_error());
		 
		  
		if($Result1) 
		{ mysql_query("commit");header("location:index.php?ajout=success"); 
		}
		else { mysql_query("rollback"); $message = colorer("Contacter la DSI, erreur création indemnisation","false");}

	}
	else 
	{
	
	if(isset($_POST["MM_insert"]) && empty($_POST['Code_indemnisation'])) $message = colorer("Vous devez saisir le code","false");
	elseif(isset($_POST["MM_insert"]) && $existe1 )
		$message = colorer("La condition indemnisation existe déjà pour le type choisi","false");
	elseif(!verifLibelle1('Code_indemnisation')) $message = colorer("Le code saisi comporte des caractères spéciaux interdits","false");
	elseif(empty($_POST['Montant_indemnisation'])) $message = colorer("Vous devez saisir le montant","false");
	elseif(!empty($_POST['Montant_indemnisation']) && !verifNumeric($_POST['Montant_indemnisation'])) $message = colorer("Le montant doit être numérique ","false");

	elseif(empty($_POST['Date_debut_indemnisation'])) $message = colorer("Vous devez saisir la date de début","false");

	elseif(isset($_POST['Date_debut_indemnisation']) && !$datedebut){
	$message = colorer("Le format de la date de début est JJ-MM-SSAA, JJ étant le jour, MM le mois, SS le siècle, AA l’année","false"); 
}elseif(isset($_POST['Date_debut_indemnisation']) && !$datevalide1){
	$message = colorer("La date de début est incorrecte ","false");
}elseif(isset($_POST['Date_fin_indemnisation']) && $_POST['Date_fin_indemnisation']!=NULL && !$datefin){
	$message = colorer("Le format de la date de fin est JJ-MM-SSAA, JJ étant le jour, MM le mois, SS le siècle, AA l’année","false"); 
}elseif(isset($_POST['Date_fin_indemnisation']) && $_POST['Date_fin_indemnisation']!=NULL && !$datevalide2){
	$message = colorer("La date de fin est incorrecte ","false");
}
	elseif(isset($_POST["MM_insert"]) && !$lesdates )$message = colorer("La date de fin doit être postérieure à la date de début","false");
	elseif(isset($_POST["MM_insert"]) && !verifLibelle('Identifiant_garantie') ) $message = colorer("Vous devez sélectionner une garantie","false");
	elseif(isset($_POST["MM_insert"]) && !verifLibelle('Identifiant_formule') ) $message = colorer("Vous devez sélectionner une formule","false");
	}


}
mysql_select_db($database_microassur, $microassur);
$query_rs = "SELECT * FROM indemnisation";
$rs = mysql_query($query_rs, $microassur) or die(mysql_error());
$row_rs = mysql_fetch_assoc($rs);
$totalRows_rs = mysql_num_rows($rs);

mysql_select_db($database_microassur, $microassur);
$query_rsgaranti = "SELECT * FROM garantie";
$rsgaranti = mysql_query($query_rsgaranti, $microassur) or die(mysql_error());
$row_rsgaranti = mysql_fetch_assoc($rsgaranti);
$totalRows_rsgaranti = mysql_num_rows($rsgaranti);

mysql_select_db($database_microassur, $microassur);
$query_rsformule = "SELECT * FROM formule";
$rsformule = mysql_query($query_rsformule, $microassur) or die(mysql_error());
$row_rsformule = mysql_fetch_assoc($rsformule);
$totalRows_rsformule = mysql_num_rows($rsformule);
/**************************************************************************************/
$query_rs_dico1 = 'SELECT Identifiant_table FROM dictionnaire where Code_table="M0014"';
$rs_dico1 = mysql_query($query_rs_dico1, $microassur) or die(mysql_error());
$row_rs_dico1 = mysql_fetch_assoc($rs_dico1);
$num_row = $row_rs_dico1['Identifiant_table'];
/**************************************************************************************/
$query_rs_dico2 = "SELECT Code_element, Nom_element FROM elementdico where Identifiant_table = '$num_row'";
$rs_dico2 = mysql_query($query_rs_dico2, $microassur) or die(mysql_error());
$row_rs_dico2 = mysql_fetch_assoc($rs_dico2);
?>
<?php  if(isset($_GET['ajout']) && $_GET['ajout']='err') $message = colorer("L'indemnisation est créée","true"); ?>
<?php if(isset($_GET['mod']) && $_GET['mod']=='success') $message = colorer("La modification de l'indemnisation est prise en compte","true"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title><?php echo $titleGen ?></title>
			<link rel="stylesheet" type="text/css" href="../../../css/main.css" />
			<link rel="stylesheet" type="text/css" href="../condition.css" />
			<!--[if IE 7]><link rel="stylesheet" type="text/css" href="../../../css/ie7.css" /><![endif]-->
            <link type="text/css" href="../../../css/custom-theme/jquery-ui-1.8.11.custom.css" rel="stylesheet" />	
            <script type="text/javascript" src="../../../js/jquery-1.5.1.min.js"></script>
            <script type="text/javascript" src="../../../js/jquery-ui-1.8.11.custom.min.js"></script>
            <script type="text/javascript" src="../../../js/jquery.ui.datepicker-fr.js"></script>
            <script type="text/javascript" src="../../../js/scripts.js"></script>
	</head>
<body>
			<div id="sf_menu1">
				<h3>Administration</h3>
				<?php
					if(isset($message))
						echo $message;
					$nommodule="Indemnisation";
					$nommodule2="Liste indemnisations";
					$nom_module_saisie="Ecran saisie";
					?>
			</div>
			<div class="cleared"></div>
		<div class="corps1">
			<!--/***************************************/-->
			<div class="imgleft"></div>
			<div class="imgmiddle">
			<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
			<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule2; ?></h5></span><span class="btnright"></span></div>
			<div class="cleared"></div>
			
			
			<table class="tab_list">
						  <tr id="niv1">
							<td class="cod_i">CODE</td>
							<td class="type_i">TYPE</td>
							<td class="montant_i">MONTANT</td>
							<td class="date_i">DATE DEBUT ET DATE FIN</td>
							<td class="gar_i">GARANTIE</td>
							<td class="for_i">FORMULE</td>
						  </tr>
             </table>						  
			
				<div class="content_table">
					<table class="tab_list" id="el">
						    <?php $i=1; do {
						  $colname_rs1 = "-1";
						if (isset($row_rs['Identifiant_garantie'])) {
						  $colname_rs1 = $row_rs['Identifiant_garantie'];
						}
						mysql_select_db($database_microassur, $microassur);
						$query_rs1 = sprintf("SELECT * FROM garantie WHERE Identifiant_garantie = %s", GetSQLValueString($colname_rs1, "text"));
						$rs1 = mysql_query($query_rs1, $microassur) or die(mysql_error());
						$row_rs1 = mysql_fetch_assoc($rs1);
						$totalRows_rs1 = mysql_num_rows($rs1);

						$colname1_rs2 = "-1";
						if (isset($row_rs['Identifiant_formule'])) {
						  $colname1_rs2 = $row_rs['Identifiant_formule'];
						}
						mysql_select_db($database_microassur, $microassur);
						$query_rs2 = sprintf("SELECT * FROM formule WHERE Identifiant_formule = %s", GetSQLValueString($colname1_rs2, "text"));
						$rs2 = mysql_query($query_rs2, $microassur) or die(mysql_error());
						$row_rs2 = mysql_fetch_assoc($rs2);
						$totalRows_rs2 = mysql_num_rows($rs2);
						   ?>
					  <?php if ($totalRows_rs > 0) { // Show if recordset not empty ?>
						<tr <?php if($i%2==0) echo 'class="colorligne"'; ?> >
						  <td class="cod_i"><a href="modindemnisation.php?Identifiant_indemnisation=<?php echo $row_rs['Identifiant_indemnisation']; ?>"><?php echo $row_rs['Code_indemnisation']; ?></a></td>
						  <td class="type_i"><a href="modindemnisation.php?Identifiant_indemnisation=<?php echo $row_rs['Identifiant_indemnisation']; ?>"><?php echo $row_rs['Type_indemnisation']; ?></a></td>
						  <td class="montant_i"><a href="modindemnisation.php?Identifiant_indemnisation=<?php echo $row_rs['Identifiant_indemnisation']; ?>"><?php echo $row_rs['Montant_indemnisation']; ?></a></td>
						  <td class="date_i"><a href="modindemnisation.php?Identifiant_indemnisation=<?php echo $row_rs['Identifiant_indemnisation']; ?>"><?php echo dateEN2FR($row_rs['Date_debut_indemnisation']); ?> / <?php if($row_rs['Date_fin_indemnisation']!=NULL) echo dateEN2FR($row_rs['Date_fin_indemnisation']); else echo $indefini; ?></a></td>
						  <td class="gar_i"><a href="modindemnisation.php?Identifiant_indemnisation=<?php echo $row_rs['Identifiant_indemnisation']; ?>"><?php echo tronque($row_rs1['Code_garantie'],10); ?></a></td>
						  <td class="for_i"><a href="modindemnisation.php?Identifiant_indemnisation=<?php echo $row_rs['Identifiant_indemnisation']; ?>"><?php echo tronque($row_rs2['Code_formule'],10); ?></a></td>
						</tr>
						<?php } // Show if recordset not empty ?>

					   <?php $i++; } while ($row_rs = mysql_fetch_assoc($rs)); ?>
					</table>
				</div>
			</div>
		<div class="imgright"></div>
			<div class="cleared"></div>
		<!--/***************************************/-->
			<div id="br"></div>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
			<div class="imgleft"></div>
				<div class="imgmiddle">
					<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
					<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nom_module_saisie; ?></h5></span><span class="btnright"></span></div>
						<div class="content_tableform1">
						  <table>
							<tr>
							  <td>CODE</td>
							  <td><input class="resize_1" type="text" maxlength="5" name="Code_indemnisation" value="<?php if(isset($_POST['Code_indemnisation']))echo remind_text('Code_indemnisation')?>" size="32" /></td>
							</tr>
							
							<tr>
							  <td>MONTANT</td>
							  <td colspan="3"><input class="td2" type="text" name="Montant_indemnisation"  maxlength="10" value="<?php if(isset($_POST['Montant_indemnisation']))echo remind_text('Montant_indemnisation')?>" size="32" /></td>
							</tr>
							<tr>
							  <td>DATE DEBUT</td>
							  <td><input name="Date_debut_indemnisation" type="text" class="date" value="<?php if(isset($_POST['Date_debut_indemnisation']))echo remind_text('Date_debut_indemnisation')?>" size="32" maxlength="10" /> </td>
							  <td class="td">DATE FIN</td>
							  <td><input name="Date_fin_indemnisation" type="text" class="date" value="<?php if(isset($_POST['Date_fin_indemnisation']))echo remind_text('Date_fin_indemnisation')?>" size="32" maxlength="10" /> </td>
							</tr>
							<tr>
							  <td>TYPE</td>
							  <td colspan="3"><select class="select" name="Type_indemnisation">
									<?php do{ ?>
												<option value="<?php echo $row_rs_dico2['Code_element']; ?>" <?php echo remind_select('Type_indemnisation', $row_rs_dico2['Code_element']) ?> ><?php echo $row_rs_dico2['Nom_element']; ?></option>
									<?php 	} while($row_rs_dico2 = mysql_fetch_assoc($rs_dico2)); ?>
							  </select>      </td>
							</tr>
							<tr>
							  <td>GARANTIE</td>
							  <td><select name="Identifiant_garantie"><option value="">Sélectionner une garantie</option>
							<?php
							do {  
							?>
									<option value="<?php echo $row_rsgaranti['Identifiant_garantie']?>"><?php echo $row_rsgaranti['Libelle_garantie']?></option>
									<?php
							} while ($row_rsgaranti = mysql_fetch_assoc($rsgaranti));
							  $rows = mysql_num_rows($rsgaranti);
							  if($rows > 0) {
								  mysql_data_seek($rsgaranti, 0);
								  $row_rsgaranti = mysql_fetch_assoc($rsgaranti);
							  }
							?>
								  </select>      </td>

								  <td class="td">FORMULE</td>
								  <td><select name="Identifiant_formule">
									<?php
							do {  
							?>
									<option value="<?php echo $row_rsformule['Identifiant_formule']?>"><?php echo $row_rsformule['Libelle_formule']?></option>
									<?php
							} while ($row_rsformule = mysql_fetch_assoc($rsformule));
							  $rows = mysql_num_rows($rsformule);
							  if($rows > 0) {
								  mysql_data_seek($rsformule, 0);
								  $row_rsformule = mysql_fetch_assoc($rsformule);
							  }
							?>
								  </select>      </td>
								</tr>
							  </table>
							</div>
					</div>
					<div class="imgright"></div>				
						<div class="cleared"></div>
						
							<div class="bt1"><a href="<?php echo $retour; ?>"><span class="btleft"></span><span class="btmiddle">Retour</span><span class="btright"></span></a></div>
							<div class="bt"><span class="btleft"></span><input type="submit" name="Creer" value="Créer" class="resizeall btmiddle"/><span class="btright"></span></div>
							
						<div class="cleared"></div>
						
  <input type="hidden" name="Identifiant_indemnisation" value="" />
  <input type="hidden" name="MM_insert" value="form1" />
	</form>
		</div>
</body>
</html><?php ob_end_flush();?>
<?php
mysql_free_result($rs);
mysql_free_result($rsgaranti);
mysql_free_result($rsformule);
mysql_free_result($rs1);
mysql_free_result($rs2);
?>
